package com.ruoyi.common.utils;

import java.io.File;
import java.io.IOException;

public class LibreOfficeUtil {

    /**
     * 将文档转换为 HTML
     *
     * @param inputFile  输入文件路径
     * @param outputFile 输出文件路径
     * @throws IOException
     * @throws InterruptedException
     */
    public static void convertToHtml(String inputFile, String outputFile) throws IOException, InterruptedException {
        // 指定 soffice 的绝对路径
        String sofficePath = "D:\\Program Files\\LibreOffice\\program\\soffice.exe";
        String command = String.format(
                sofficePath+
                " --headless --convert-to html %s --outdir %s",
                inputFile,
                new File(outputFile).getParent()
        );
        Process process = Runtime.getRuntime().exec(command);
        process.waitFor();
    }

    /**
     * 将文档转换为 PDF
     *
     * @param inputFile  输入文件路径
     * @param outputFile 输出文件路径
     * @throws IOException
     * @throws InterruptedException
     */
    public static void convertToPdf(String inputFile, String outputFile) throws IOException, InterruptedException {
        String sofficePath = "D:\\Program Files\\LibreOffice\\program\\soffice.exe";
        String command = String.format(
                sofficePath+
                " --headless --convert-to pdf %s --outdir %s",
                inputFile,
                new File(outputFile).getParent()
        );
        Process process = Runtime.getRuntime().exec(command);
        process.waitFor();
    }
}